<p>This rule raises an issue when code uses <code>object.length - index</code> calculations in methods that support negative indexing, such as
<code>slice()</code>, <code>at()</code>, <code>splice()</code>, and others.</p>
<h2>Why is this an issue?</h2>
<p>JavaScript provides negative indexing for many array and string methods, which allows accessing elements from the end of a collection using
negative numbers. When you write <code>array.slice(array.length - 2, array.length - 1)</code>, you’re doing unnecessary calculations that make the
code harder to read and understand.</p>
<p>Negative indices are more concise and express the intent more clearly. The expression <code>-2</code> immediately tells you "second from the end",
while <code>array.length - 2</code> requires mental calculation to understand the same concept.</p>
<p>Using negative indices also reduces the risk of errors. With <code>array.length - index</code> calculations, you might accidentally use the wrong
variable name or make arithmetic mistakes. Negative indices eliminate these potential sources of bugs.</p>
<p>Additionally, negative indices are more performant since they avoid the property access to <code>.length</code> and the subtraction operation,
though this performance difference is typically negligible in practice.</p>
<h3>What is the potential impact?</h3>
<p>This issue affects code readability and maintainability. While it doesn’t cause runtime errors, it makes code unnecessarily verbose and harder to
understand. The extra <code>.length</code> property accesses and arithmetic operations also add minor performance overhead.</p>
<h3>How to fix?</h3>
<p>Replace <code>object.length - n</code> with <code>-n</code> in method calls that support negative indexing. This works for methods like
<code>slice()</code>, <code>at()</code>, <code>splice()</code>, and others on arrays, strings, and typed arrays.</p>
<h4>Non-compliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
const lastTwo = array.slice(array.length - 2); // Noncompliant
const lastElement = array.at(array.length - 1); // Noncompliant
array.splice(array.length - 1, 1); // Noncompliant
</pre>
<h4>Compliant code example</h4>
<pre data-diff-id="1" data-diff-type="compliant">
const lastTwo = array.slice(-2);
const lastElement = array.at(-1);
array.splice(-1, 1);
</pre>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://github.com/sindresorhus/eslint-plugin-unicorn#readme">eslint-plugin-unicorn</a> - Rule <a
  href="https://github.com/sindresorhus/eslint-plugin-unicorn/blob/HEAD/docs/rules/prefer-negative-index.md">prefer-negative-index</a> </li>
  <li> MDN - Array.prototype.slice() - <a
  href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice">Documentation for the Array slice method,
  including negative index usage</a> </li>
  <li> MDN - Array.prototype.at() - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at">Documentation
  for the Array at method with negative indexing examples</a> </li>
  <li> MDN - String.prototype.slice() - <a
  href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice">Documentation for the String slice method and
  negative indices</a> </li>
</ul>
<h3>Related Rules</h3>
<ul>
  <li> unicorn/prefer-at - <a href="https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-at.md">ESLint rule that prefers
  using the at() method for array element access</a> </li>
</ul>
